﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ECommerce.Library.DataStructure
{
    public class ReadOnlyCustomCollectionEnumerator<TGenericType> : IEnumerator<TGenericType>
    {
        private ReadOnlyCustomCollection<TGenericType> _items;
        private int _index;

        public ReadOnlyCustomCollectionEnumerator(ReadOnlyCustomCollection<TGenericType> items)
        {
            this._index = -1;
            this._items = items;
        }

        #region IEnumerator<TGenericType> Members

        public TGenericType Current
        {
            get
            {
                try
                {
                    return this._items[this._index];
                }
                catch (IndexOutOfRangeException ex)
                {
                    throw new InvalidOperationException("Failed to get the current item", ex);
                }
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this._index = -1;
        }

        #endregion

        #region IEnumerator Members

        object System.Collections.IEnumerator.Current
        {
            get
            {
                try
                {
                    return this._items[this._index];
                }
                catch (IndexOutOfRangeException ex)
                {
                    throw new InvalidOperationException("Failed to get the current item", ex);
                }
            }
        }

        public bool MoveNext()
        {
            this._index++;
            return (this._index < this._items.Count);
        }

        public void Reset()
        {
            this._index = -1;
        }

        #endregion
    }
}
